/**
 * Created by steve on 14-9-18.
 */

// 配置
require.config({
    paths: {
        jquery: '../libs/jquery-1.11.1/jquery-1.11.1.min',
        jquerymobile: '../libs/jquery.mobile-1.4.4/jquery.mobile-1.4.4.min',
        net: './net',
        md5: './md5',
        logon: './logon',
        newsroom: './newsroom',
        active:'./active',
        forget_password: './forget_password',
        newroom_detailed: './newsroomDetailed',
        phonegap: '../phonegap',
        dialogs:'./dialogs',
        filter:'./filter',
        setting:'./setting',
        activity_register:'./activity_register',
        activities:'./activities',
        activity_details:'./activity_details',
        activity_verify:'./activity_verify',
        activity_confirm:'./activity_confirm',
        activity_filter:'./activity_filter'
    }
});

require(['logon', 'newsroom', 'active', 'forget_password', 'newroom_detailed', 'filter', 'setting', 'infomation', 'question', 'policy', 'activities', 'activity_details', 'activity_register', 'activity_verify', 'activity_confirm', 'activity_filter'], function(phonegap) {
    // 全局变量开启
    window.q = {};
	// Fix menu scrolling issue
	$("body").on("touchmove", ".ui-panel-open, .ui-panel-dismiss-open", function(e) {
		e.preventDefault();
	});
	
	//Bind keypress chars limited control event for all username and password input field
	$("#active_txt_username,#active_pass_password,#change_password-txt-currentPassword,#change_password-txt-newPassword,#change_password-txt-newPassword-confirm,#logon_txt_username,#logon_txt_password").on("keypress",function(event){
		if(!event){
			event = window.event;
		}
		if(event.currentTarget.value.length+1>15){
			if(event.keyCode != 8){
				event.returnValue = false;
				return false;
			}
		}else{
			return true;
		}
	});
	
	window.setBodyOverflow = function(body){
		if(window.navigator.userAgent.substr(window.navigator.userAgent.indexOf('Android') + 8, 3) < 4.0){
			body.css('overflow-y','hidden !important');
		}else{
			body.css('overflow-y','hidden');
		}
	}
	
	window.historyView = [];
	var menuTo = {
		logon : "",
		newsroom : '#panel_menu',
		setting: '#setting_panel_menu',
		activities : '#activities_panel_menu'
	};
	
	$.mobile.newChangePage = function(to, options){
		historyView.unshift('#'+$('.ui-page-active').attr('id'));
		$.mobile.changePage(to, options);
	}
	
	changeMenuBackFromGreenLaiSee = function( id ){
		id = id.replace('push_','#');
		if(id == '#AR'){
			cordova.exec(function(){console.log("success")}, function(){console.log("error")}, "ShowVideoPlaybackView", "showVideoPlaybackView", []);
		}else $.mobile.newChangePage(id,{ transition: "slide",reverse: false,changeHash: false});
	}
	
	//Compatible with render code version  
	$.mobile.backChangePage = function(to, options){
		historyView.shift();
		$.mobile.changePage(to, { transition: "slide",reverse: true,changeHash: false});
	}

	$.mobile.goBackPage = function(){
		$.mobile.changePage(historyView.shift(), { transition: "slide",reverse: true,changeHash: false});
	}
	
	document.addEventListener("backbutton", function(e){
		if($('#errorAlert').length <= 0){
			if(window.historyView.length <= 0){
					var menuId = $('.ui-page-active').attr('id');
					var menu = menuTo[menuId];
					if($(menu).hasClass('ui-panel-closed')){
						$('#'+menuId+'_btn_menu').click();
					}else{
						//exitapp
					}
			}else $.mobile.changePage(window.historyView.shift(), { transition: "slide",reverse: true,changeHash: false});
		}
	});
	// Nick added for pull to refresh start
	window.shouldPageRefresh = {
		newsroom: true,
		activities: true
	};

	window.getVendorStyle = function(style) {
		var dummyStyle = document.createElement("div").style,
		    vendor = (function() {
				var vendors = "t,webkitT,MozT,msT,OT".split(","),
					t,
					i = 0,
					l = vendors.length;

				for (; i < l; i++) {
					t = vendors[i] + "ransform";
					if (t in dummyStyle) {
						return vendors[i].substr(0, vendors[i].length - 1);
					}
				}

				return false;
			})();
		vendor ? "-" + vendor.toLowerCase() + "-" : "";

		if (vendor === "") return style;

		style = style.charAt(0).toUpperCase() + style.substr(1);
		return vendor + style;
	};
	// Nick added for pull to refresh end
	// Nick added for multiple images start
	window.imageList = [];
	// Nick added for multiple images end
});